package com.zrf.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.zrf.pojo.Leave;

@Repository("leaveDao")
public class LeaveDaoImpl extends BaseDaoImpl<Leave>{

	private Session session = null;
	public void add(Leave l) {
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			Transaction tr = session.beginTransaction();
			session.save(l);
			tr.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
	}
	public void update(Leave l) {
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			Transaction tr = session.beginTransaction();
			session.update(l);
			tr.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
	}
	public List<?> findByName(String name) {
		// TODO Auto-generated method stub
		List<?> list = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			list = session.createQuery("FROM Leave l WHERE l.person.pname=?").setString(0, name).list();
			if (list!=null&&list.size()<1) {
				list = null;
			}
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
		return list;
	}
}
